//
//  HHPHotLiveCell.m
//  HHPLiveVideo
//
//  Created by 韩红坡 on 2017/4/19.
//  Copyright © 2017年 韩红坡. All rights reserved.
//

#import "HHPHotLiveCell.h"
#import "HHPLiveModel.h"

@interface HHPHotLiveCell ()

@property (nonatomic, strong) UIView *topView;
/** 头像 */
@property (nonatomic, strong) UIImageView *headImageView;
/** 直播名 */
@property (nonatomic, strong) UILabel *nameLabel;
/** 星级 */
@property (nonatomic, strong) UIImageView *starView;
/** 地区 */
@property (nonatomic, strong) UIButton *locationButton;
/** 观众 */
@property (nonatomic, strong) UILabel *audienceLabel;
/** 大的预览图 */
@property (nonatomic, strong) UIImageView *bigPicView;
/** 直播图标 */
@property (nonatomic, strong) UIImageView *liveImageView;

@end

@implementation HHPHotLiveCell
#pragma mark - life cycle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = RGB(234, 232, 237);
        
        [self initViews];
    }
    return self;
}

#pragma mark - GMTableViewCellProtocol
- (void)setCellModel:(HHPLiveModel *)cellModel
{
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:cellModel.smallpic] placeholderImage:[UIImage imageNamed:@"placeholder_head"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.headImageView.image = [image imageByRoundCornerRadius:image.size.width borderWidth:2 borderColor:[UIColor purpleColor]];
    }];

    self.nameLabel.text = cellModel.myname;
    
    CGFloat width = [cellModel.myname sizeWithAttributes:@{NSFontAttributeName:Font(16)}].width + 2;
    [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width).with.priorityLow();
    }];
    
    // 如果没有地址, 给个默认的地址
    if (!cellModel.gps.length) {
        cellModel.gps = @"喵星";
    }
    
    [self.locationButton setTitle:cellModel.gps forState:UIControlStateNormal];
    [self.bigPicView sd_setImageWithURL:[NSURL URLWithString:cellModel.bigpic] placeholderImage:[UIImage imageNamed:@"profile_user"]];
    self.starView.image  = cellModel.starImage;
    self.starView.hidden = !cellModel.starlevel;

    // 设置当前观众数量
    NSString *fullAudience = [NSString stringWithFormat:@"%ld人在看", cellModel.allnum];
    NSRange range = [fullAudience rangeOfString:[NSString stringWithFormat:@"%ld", cellModel.allnum]];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:fullAudience];
    [attr addAttribute:NSFontAttributeName value:Font(14) range: range];
    [attr addAttribute:NSForegroundColorAttributeName value:RGB(216, 41, 116) range:range];
    self.audienceLabel.attributedText = attr;
}

#pragma mark - private methods
- (void)initViews
{
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.headImageView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.starView];
    [self.contentView addSubview:self.locationButton];
    [self.contentView addSubview:self.audienceLabel];
    [self.contentView addSubview:self.bigPicView];
    [self.contentView addSubview:self.liveImageView];
    
    [self layoutPageSubviews];
}

- (void)layoutPageSubviews
{
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.width.equalTo(self.contentView);
        make.height.mas_equalTo(55);
    }];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(KScaleY(5));
        make.left.equalTo(self.contentView).offset(KScaleX(10));
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImageView);
        make.left.equalTo(self.headImageView.mas_right).offset(KScaleX(10));
        make.width.mas_lessThanOrEqualTo(100).with.priorityHigh();
    }];
    
    [self.starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(KScaleX(5));
        make.centerY.equalTo(self.nameLabel);
    }];
    
    [self.locationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(KScaleY(5));
        make.left.equalTo(self.nameLabel);
    }];
    
    [self.audienceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(KScaleX(-10));
        make.left.equalTo(self.starView.mas_right).offset(KScaleX(10));
        make.centerY.equalTo(self.headImageView);
    }];
    
    [self.bigPicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.left.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(KScaleY(-5));
    }];
    
    [self.liveImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bigPicView).offset(KScaleY(10));
        make.right.equalTo(self.contentView).offset(KScaleX(-10));
    }];
}

#pragma mark - getter and setter
- (UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = [UIColor whiteColor];
    }
    
    return _topView;
}

- (UIImageView *)headImageView
{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeholder_head"]];
    }
    
    return _headImageView;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = Font(16);
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.adjustsFontSizeToFitWidth = YES;
    }
    
    return _nameLabel;
}

- (UIImageView *)starView
{
    if (!_starView) {
        _starView = [[UIImageView alloc] init];
        _starView.contentMode = UIViewContentModeScaleAspectFit;
    }
    
    return _starView;
}

- (UIButton *)locationButton
{
    if (!_locationButton) {
        _locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_locationButton setImage:[UIImage imageNamed:@"home_location"] forState:UIControlStateNormal];
        [_locationButton setTitleColor:RGB(111, 113, 121) forState:UIControlStateNormal];
        _locationButton.titleLabel.font = Font(12);
    }
    
    return _locationButton;
}

- (UILabel *)audienceLabel
{
    if (!_audienceLabel) {
        _audienceLabel = [[UILabel alloc] init];
        _audienceLabel.font = Font(13);
        _audienceLabel.textColor = RGB(111, 113, 121);
        _audienceLabel.textAlignment = NSTextAlignmentRight;
    }
    
    return _audienceLabel;
}

- (UIImageView *)bigPicView
{
    if (!_bigPicView) {
        _bigPicView = [[UIImageView alloc] init];
    }
    
    return _bigPicView;
}

- (UIImageView *)liveImageView
{
    if (!_liveImageView) {
        _liveImageView = [[UIImageView alloc] init];
        _liveImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    
    return _liveImageView;
}

@end
